/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;

using DslEditorPowerToy.VisualStudio;

using EnvDTE;
using EnvDTE80;

namespace DslEditorPowerToy.Recipes.ValueProviders
{
    /// <summary>
    /// Returns the specified property of the project
    /// </summary>
    public class ProjectPropertyValueProvider : ValueProviderBase
    {
        private string _propertyName = string.Empty;
        private string _projectArgumentName = string.Empty;

        #region Constructors
        public ProjectPropertyValueProvider()
            : base()
        {
        }
        #endregion

        #region Public Methods
        public override void Configure(StringDictionary attributes)
        {
            if (false == attributes.ContainsKey("ProjectArgument"))
                throw new ArgumentNullException("ProjectArgument");

            this._projectArgumentName = attributes["ProjectArgument"];

            if (false == attributes.ContainsKey("Property"))
                throw new ArgumentNullException("Property");

            this._propertyName = attributes["Property"];
        }
        public override bool OnArgumentChanged(string changedArgumentName, object changedArgumentValue, object currentValue, out object newValue)
        {
            newValue = GetValue();
            return true;
        }
        public override bool OnBeginRecipe(object currentValue, out object newValue)
        {
            if (currentValue != null)
            {
                // Do not assign a new value, and return false to flag that 
                // we don't want the current value to be changed.
                newValue = null;
                return false;
            }

            newValue = GetValue();
            return true;
        }
        public override bool OnBeforeActions(object currentValue, out object newValue)
        {
            return OnBeginRecipe(currentValue, out newValue);
        }
        #endregion

        #region Private Methods
        /// <summary>
        /// Gets the calcualted value
        /// </summary>
        /// <returns></returns>
        private string GetValue()
        {
            //Dereference project argument
            Project project = (Project)this.GetArgumentValue(this._projectArgumentName);
            if (null == project)
                return null;

            //Get property from project
            object propertyValue = DteHelper.GetProjectProperty(project, this._propertyName);
            if (null != propertyValue)
                return propertyValue as string;

            return null;
        }
        #endregion
    }
}
